//判断登录状态
var username = getCookie('username');
$(() => {
	if (username) {
		$('.Tnav_info a:first').text(`欢迎! ${username}`);
		$('.Tnav_info a:first').attr('href', './userinfo.html');
		$('.Tnav_info a:eq(1)').text('退出');
		$('.Tnav_info a:eq(1)').attr('href', './login.html');
	}
})
//购物车的实现
$(() => {
	//购物车的点击事件
	$('.buy_cart').click(() => {
		if (!username) {
			layer.msg('您还没登录,即将跳转到登录页面', {
				time: 1500
			}, () => {
				localStorage.setItem('url', location.href);
				location.href = './login.html'
			});
			return;
		}
		location.href = './shopcart.html';
	});
	$('.buy_cart span').text('');
	$('.shoppingCart').click(() => {
		if (!username) {
			layer.msg('您还没登录,即将跳转到登录页面', {
				time: 1500
			}, () => {
				localStorage.setItem('url', location.href);
				location.href = './login.html'
			});
			return;
		}
		location.href = './shopcart.html';
	});
})

//下拉搜索框的实现
//调用函数
$(() => {
	searchList('#search', '.searchList');
})
//搜索框点击事件
$(() => {
	$('.search_icon').click(() => {
		location.href = './list.html';
	});
})
//用户中心点击事件
$(() => {
	$('.userinfo').click(() => {
		location.href = './userinfo.html';
	})
})
//给登录注册页面绑定点击事件
//保存当前网址URL到本地存储器
$(() => {
	$('.Tnav_info a').each(function (i) {
		if (i == 2) return;
		if (username && i == 0) return;
		this.onclick = () => {
			localStorage.setItem('url', location.href);
		}
	})
})
//封装函数
function searchList(tag, subTag) {
	//给搜索框添加获取焦点事件
	$(tag).focus(function () {
		//获取下拉框标签的节点对象
		let txtList = $(subTag);
		//更改列表样式 显示下拉列表
		txtList.css({
			display: 'block'
		});
		//给列表设置鼠标点击的委托事件
		txtList.click((eve) => {
			//将点击到的搜索值赋给搜索框
			this.value = eve.target.innerHTML;
		});
		//给列表设置鼠标移入的委托事件
		txtList.mouseover((eve) => {
			//将点击到的搜索值赋给搜索框
			this.value = eve.target.innerHTML;
		});
	});
	//给搜索框添加失去焦点事件
	$(tag).blur(() => {
		//失去焦点时隐藏下拉框
		$(subTag).css({
			display: 'none'
		});
	})
}
//侧边导航栏颜色切换效果
function onLeaver_1() {
	getID("Rnav_img_1").src = "./image/aside/0.png";
	getID("Rnav_txt_1").style.color = "grey";
}
function onMover_1() {
	getID("Rnav_img_1").src = "./image/aside/1.png";
	getID("Rnav_txt_1").style.color = "orange";
}
function onLeaver_2() {
	getID("Rnav_img_2").src = "./image/aside/2.png";
	getID("Rnav_txt_2").style.color = "grey";
}
function onMover_2() {
	getID("Rnav_img_2").src = "./image/aside/3.png";
	getID("Rnav_txt_2").style.color = "orange";
}
function onLeaver_3() {
	getID("Rnav_img_3").src = "./image/aside/4.png";
	getID("Rnav_txt_3").style.color = "grey";
}
function onMover_3() {
	getID("Rnav_img_3").src = "./image/aside/5.png";
	getID("Rnav_txt_3").style.color = "orange";
}
function onLeaver_4() {
	getID("Rnav_img_4").src = "./image/aside/6.png";
	getID("Rnav_txt_4").style.color = "grey";
}
function onMover_4() {
	getID("Rnav_img_4").src = "./image/aside/7.png";
	getID("Rnav_txt_4").style.color = "orange";
}
function onLeaver_5() {
	getID("Rnav_img_5").src = "./image/aside/8.png";
	getID("Rnav_txt_5").style.color = "grey";
}
function onMover_5() {
	getID("Rnav_img_5").src = "./image/aside/9.png";
	getID("Rnav_txt_5").style.color = "orange";
}
function onLeaver_6() {
	getID("Rnav_img_6").src = "./image/aside/10.png";
	getID("Rnav_txt_6").style.color = "grey";
}
function onMover_6() {
	getID("Rnav_img_6").src = "./image/aside/11.png";
	getID("Rnav_txt_6").style.color = "orange";
}
function getID(tag) {
	return document.getElementById(tag);
}

//返回顶部按钮效果
function goTop() {
	var scrollToTop = setInterval(function () {
		var pos = pageYOffset;
		if (pos > 0) {
			scrollTo(0, pos - 40);
		} else {
			clearInterval(scrollToTop);
		}
	}, 8);
}
//侧边导航栏滚动效果
function Rnav_scroll() {
	var n = document.documentElement.scrollTop;
	if (n < 600) {
		document.getElementById('goTop').style.display = "none";
	}
	else {
		document.getElementById('goTop').style.display = "block";
	}
}

onscroll = Rnav_scroll; //页面滚动时，调用侧边栏滚动函数